using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandyIRCChat
{
    public partial class ChannelForm : Form, IDisposable
    {
        private HandyLogic handyLogic;

        private ChannelObject channelObj;

        private delegate void SetOutputMessage(string text);

        private delegate void CloseFormDelegate();

        public ChannelForm(HandyLogic hl, ChannelObject obj)
        {
            handyLogic = hl;
            channelObj = obj;
            channelObj.ChannelForm = this;

            InitializeComponent();

            channelIn.KeyDown += new KeyEventHandler(OnChannelInKeyDown);
            channelObj.ChannelMessageEvent += 
                new ChannelObject.ChannelMessageEventHandler(ChannelObj_ChannelMessage);
            handyLogic.LeaveChannelEvent += 
                new HandyLogic.LeaveChannelEventHandler(handyLogic_LeaveChannelEvent);

            if (channelObj.MessageBuffer.Length != 0)
                WriteOutputMessage(channelObj.MessageBuffer);

            this.Text = obj.Channel;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            channelIn.Focus();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            channelObj.ChannelMessageEvent -=
                new ChannelObject.ChannelMessageEventHandler(ChannelObj_ChannelMessage);

            channelObj.ChannelForm = null;

            handyLogic.Options.ChannelWindowState.LocX = this.Location.X;
            handyLogic.Options.ChannelWindowState.LocY = this.Location.Y;
            handyLogic.Options.ChannelWindowState.SizeX = this.Size.Width;
            handyLogic.Options.ChannelWindowState.SizeY = this.Size.Height;

            base.OnClosing(e);
        }

        protected override void OnCreateControl()
        {
            this.SetBounds(handyLogic.Options.ChannelWindowState.LocX,
                handyLogic.Options.ChannelWindowState.LocY,
                handyLogic.Options.ChannelWindowState.SizeX,
                handyLogic.Options.ChannelWindowState.SizeY);

            base.OnCreateControl();
        }

        private void WriteOutputMessage(string text)
        {
            if (this.channelOut.InvokeRequired)
            {
                SetOutputMessage d = new SetOutputMessage(WriteOutputMessage);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                channelOut.AppendText(text);
                channelOut.ScrollToCaret();
            }
        }

        private void ChannelObj_ChannelMessage(object sender, string message)
        {
            WriteOutputMessage(message);
        }

        private void CloseForm()
        {
            this.Close();
        }

        private void handyLogic_LeaveChannelEvent(object sender, string channelName)
        {
            if (this.InvokeRequired)
            {
                if (channelName == channelObj.Channel)
                {
                    CloseFormDelegate d = new CloseFormDelegate(CloseForm);
                    this.Invoke(d, new object[] { });
                }
            }
            else
                this.Close();
        }

        private void OnChannelInKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (channelIn.Text.Length == 0)
                    return;

                StringBuilder sb = new StringBuilder(channelIn.Text);
                if (sb[0] == '\n')
                    sb.Remove(0, 1);

                channelObj.SendMessage(sb.ToString());
                channelIn.Clear();
            }
        }
    }
}